<template>
  <div id="app">
    <h3>用户管理</h3> 
    <div class="container">
      <el-dialog
        v-model="dialogFormVisible"
        title="Shipping address"
        width="500"
      >
        <el-form :model="form" label-width="120px">
          <el-form-item label="姓名">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="年龄">
            <el-input v-model="form.age"></el-input>
          </el-form-item>
          <el-form-item label="地址">
            <el-input v-model="form.agge"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="addUser">添加</el-button>
            <el-button type="primary" @click="updateUser">更新</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
    <div class="inp">
      <el-input
        v-model="searchKeyword"
        placeholder="请输入查询关键字"
        style="width: 300px; height: 40px; margin-right: 150px; margin-bottom: 15px;"
      ></el-input>
    </div>
    <el-table :data="filteredUsers" style="width: 100%">
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="agge" label="地址"></el-table-column>
      <el-table-column label="操作">
        <template #default="scope" style="text-align: center;">
          <el-button size="mini" @click="editUser(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" @click="showUserDetails(scope.$index, scope.row)">详情</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.$index)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 用户详情 -->
    <el-dialog
      v-model="detailsDialogVisible"
      title="用户详情"
      width="500"
    >
      <el-form :model="detailsForm" label-width="120px">
        <el-form-item label="姓名">
          <el-input v-model="detailsForm.name" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="detailsForm.age" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="detailsForm.agge" :disabled="true"></el-input>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { ref, computed, onMounted } from "vue";
import { ElMessage } from "element-plus";

export default {
  name: "App",
  setup() {
    const dialogFormVisible = ref(false);
    const form = ref({
      name: "",
      age: "",
      agge:'',
      index: -1, // 用于记录当前编辑的用户索引
    });
    let users = ref([]);

    // 从 localStorage 获取数据
    const loadData = () => {
      const storedUsers = localStorage.getItem("users");
      if (storedUsers) {
        users.value = JSON.parse(storedUsers);
      }
    };

    // 保存数据到 localStorage
    const saveData = () => {
      localStorage.setItem("users", JSON.stringify(users.value));
    };

    // 添加用户
    const addUser = () => {
      if (form.value.name && form.value.age) {
        users.value.push({ ...form.value });
        form.value.name = "";
        form.value.age = "";
        form.value.index = -1; // 重置索引
        saveData(); // 保存数据到 localStorage
      } else {
        ElMessage.error("请输入姓名和年龄");
      }
      dialogFormVisible.value = false;
    };

    // 编辑用户
    const editUser = (index, row) => {
      form.value = { ...row };
      form.value.index = index; // 记录当前编辑的用户索引
      dialogFormVisible.value = true;
    };

    // 更新用户
    const updateUser = () => {
      if (form.value.index !== -1) {
        users.value.splice(form.value.index, 1, { ...form.value });
        form.value.name = "";
        form.value.age = "";
        form.value.index = -1; // 重置索引
        saveData(); // 保存数据到 localStorage
      }
      dialogFormVisible.value = false;
    };

    // 删除用户
    const deleteUser = (index) => {
      users.value.splice(index, 1);
      saveData(); // 保存数据到 localStorage
    };

    // 查询关键字
    const searchKeyword = ref("");

    // 过滤后的用户数据
    const filteredUsers = computed(() => {
      return users.value.filter((user) => {
        return user.name.includes(searchKeyword.value);
      });
    });

    // 用户详情对话框
    const detailsDialogVisible = ref(false);
    const detailsForm = ref({
      name: "",
      age: "",
      agge:""
    });

    // 显示用户详情
    const showUserDetails = (index, row) => {
      detailsForm.value = { ...row };
      detailsDialogVisible.value = true;
    };

    onMounted(() => {
      loadData(); // 加载数据
    });

    return {
      dialogFormVisible,
      form,
      users,
      addUser,
      deleteUser,
      editUser,
      updateUser,
      searchKeyword,
      filteredUsers,
      detailsDialogVisible,
      detailsForm,
      showUserDetails,
    };
  },
};
</script>

<style lang="scss" scoped>
#app {
  .container {
    margin-bottom: 15px;
  }
  .inp {
    width: 100%;
    display: flex;
    justify-content: right;
  }
  .example-pagination-block {
    width: 50%;
    margin: auto;
    margin-top: 20px;
  }
  .el-table .cell{
    text-align: center;
  }
}
</style>
